﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAL.Entities;
using System.Data.SqlClient;

namespace DAL.EntitiesDAL
{
    public class TacGiaDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Tacgia");
        }

       
        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Tacgia {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (TacGia)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Tacgia '{0}', N'{1}', N'{2}', '{3}' ";
            sql = string.Format(sql, o.MATACGIA, o.BUTDANH, o.HOTACGIA, o.GIOITINH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (TacGia)obj;
            var sql = "set dateformat dmy; execute sp_Update_Tacgia '{0}', N'{1}', N'{2}', '{3}' ";
            sql = string.Format(sql, o.MATACGIA, o.BUTDANH, o.HOTACGIA, o.GIOITINH);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }
       
        public DataTable Search(string name)
        {
            var sql = @"Select *, (Case when GIOITINH=0 then 'Nam' else N'Nữ' end) as Sex from tb_Tacgia";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.TacGia o)
        {
            string sql;
            if (o.MATACGIA != "")
                sql = @"Select MATACGIA, BUTDANH, HOTACGIA + ' ' + TENTACGIA as HOVATEN, (Case when GIOITINH=0 then 'Nam' else N'Nữ' end) as Sex from tb_Tacgia Where MATACGIA like '%" + o.MATACGIA + "%' ";
            else
                sql = @"Select MATACGIA, BUTDANH, HOTACGIA + ' ' + TENTACGIA as HOVATEN, (Case when GIOITINH=0 then 'Nam' else N'Nữ' end) as Sex from tb_Tacgia Where BUTDANH like N'" + o.BUTDANH + "' or (HOTACGIA like substring(N'" + o.HOTACGIA + "', 0, len(N'"+o.HOTACGIA+"') - len(Reverse(substring(Reverse(N'" + o.HOTACGIA + "'), 0, Patindex('% %',Reverse(N'" + o.HOTACGIA + "'))))) )  and TENTACGIA like Reverse(substring(Reverse(N'" + o.HOTACGIA + "'), 0, Patindex('% %',Reverse(N'" + o.HOTACGIA + "'))) ) )";
            return ExecuteQuery(sql);
        }


        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(MATACGIA) as Ma From tb_Tacgia";
            return ExecuteQuery(sql);
        }

      
        #endregion
    }
}
